(function(){

'use strict';

angular.module('controllers.oldOMDisplay.infoDispaly')
  .controller('twoBusinessController', twoBusinessController);

function twoBusinessController($scope, COREDATA, InfoViewService, CoreService,CoreLoading) {
  $scope.flage = 1;
  $scope.shoumun = 1;
  $scope.startTime = new Date();
  $scope.endTime = new Date();
  //省份的数据
  $scope.provinces = COREDATA.PROVINCES;
  //业务的模拟数据
  $scope.twoBusiness = [{
    businessName: '缴费',
    businessCode: '02'
  }, {
    businessName: '签约',
    businessCode: '01'
  }];
  $scope.searchInfo = {
    provinceCode: [],
    businessCode: '02',
    year: {
      startTime: '20141',
      endTime: '20152'
    },
    startTime: new Date(new Date().setMonth(new Date().getMonth() - 3 * 9)),
    endTime: new Date(new Date().setMonth(new Date().getMonth() - 3))
  };
  $scope.searchData = function() {
    $scope.shoumun == 1 ? $scope.shoumun = 0 : $scope.shoumun = 1;
    var params = {};
    var start = splitTime($scope.searchInfo.year.startTime);
    var end = splitTime($scope.searchInfo.year.endTime);
    if ($scope.shoumun === 0) {
      params.startTime = start.year;
      params.startQuarter = start.quarter;
      params.endTime = end.year;
      params.endQuarter = end.quarter;
      params.provinceCodes = $scope.searchInfo.provinceCode.map(function(item){
        return item.provinceCode;
      }); 
      params.timeType = '4';
      CoreLoading.setState(true);
      InfoViewService.queryTwoBusiness(params, searchBusinessData);
    }
  };


  /**
   * 季度字符串,解析为年度和季度字段
   * @param quarterString 季度字符串
   * @returns {{}} 返回季度，年份的对象
   */
  function splitTime(quarterString) {
    var yearQuarter = {};
    var year = quarterString.substring(0, 4);
    var quarter = quarterString.substring(4, 5);
    yearQuarter.year = year;
    yearQuarter.quarter = quarter;
    return yearQuarter;
  }



  var title, legendData,titleName;
  var addCustomer = [];
  var zeroCustomer = [];
  var netCustomer = [];
  var time = [];
  var displayData = [];

  function searchBusinessData(res) {
    CoreLoading.setState(false);
    if (res.state = '10000') {
      $scope.flagObj = true;

      angular.forEach($scope.provinces, function(province) {
        if ($scope.searchInfo.provinceCode === province.provinceCode) {
          $scope.provinceName = province.provinceName;
        }
      });
      
      $scope.provinceName = $scope.searchInfo.provinceCode.length === 0 ? '全国': $scope.searchInfo.provinceCode.map(function(item){
        return item.provinceName;
      }).join('、');
      // !$scope.provinceName && ($scope.provinceName = '全国');
      angular.forEach($scope.twoBusiness, function(business) {
        if ($scope.searchInfo.businessCode === business.businessCode) {
          $scope.businessName = business.businessName;
        }
      });      
      var seriesData = [];
      if ($scope.searchInfo.businessCode === '02') {
        title = $scope.provinceName + $scope.businessName + '新增、零次、净增客户数统计';
        angular.forEach(res.data, function(item) {
          addCustomer.push(item.addCustomer);
          zeroCustomer.push(item.zeroCustomer);
          netCustomer.push(item.netCustomer);
          time.push(item.time);
        });
        displayData.push(addCustomer);
        displayData.push(zeroCustomer);
        displayData.push(netCustomer);
        legendData = ['新增客户数', '零次客户数', '净增客户数'];
        setSeries(legendData);

      } else if ($scope.searchInfo.businessCode === '01') {
        title = $scope.provinceName + $scope.businessName + '新增、净增客户数统计';
        angular.forEach(res.data, function(item) {
          addCustomer.push(item.addCustomer);
          zeroCustomer.push(item.zeroCustomer);
          time.push(item.time);
        });
        displayData.push(addCustomer);
        displayData.push(zeroCustomer);
        legendData = ['新增客户数', '零次客户数'];
        setSeries(legendData);
      }

      var customerEchart = echarts.init(document.getElementById('customer'));
      var options = {
        title: {
          text: title,
          x: 'center'
        },
        grid: {
          x2: '13%'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          orient: 'vertical',
          x: 'right',
          y: 'center',
          data: legendData
        },
        dataZoom: {
          show: true,
          realtime: true,
          start: 0,
          end: 100
        },
        // calculable: true,
        xAxis: [{
          type: 'category',
          data: time
        }],
        yAxis: [{
          name: '(人数)',
          axisLine: {
            onZero: false
          },
          type: 'value'
        }],
        series: seriesData
      };
      customerEchart.setOption(options);


    } else {
      CoreService.dialog('系统错误');
    }

    function setSeries(param) {
      var itemData;
      for (var i = 0; param.length > i; i++) {
        itemData = {
          name: '',
          type: 'line',
          data: []
        };
        itemData.name = param[i];
        itemData.data = displayData[i];
        seriesData.push(itemData);
      }
    }

  }
  $scope.searchData();
}
})();